package com.cq.jackmar.rxdemo.http.subscriber;

import android.app.Dialog;
import android.content.Context;

import com.cq.jackmar.rxdemo.view.customdialog.DialogMaker;
import com.cq.jackmar.rxdemo.view.customdialog.ProgressCircleDialogHandler;
import com.cq.jackmar.rxdemo.view.customdialog.ProgressDialogHandler;

import rx.Subscriber;

/**
 * @Title
 * @Author JackMar
 * @Date 2017-03-10 17:14
 */
public class ProgressSubscriber<T> extends Subscriber<T> implements DialogMaker.DialogCallBack {

    private IOnNextListener mIOnNextListener;
    private ProgressCircleDialogHandler mProgressDialogHandler;

    private Context context;

    public ProgressSubscriber(Context context, IOnNextListener mIOnNextListener) {
        this.mIOnNextListener = mIOnNextListener;
        this.context = context;
        mProgressDialogHandler = new ProgressCircleDialogHandler(context, this, true);
    }

    private void showProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     */
    @Override
    public void onStart() {
        showProgressDialog();
    }

    /**
     * 完成，隐藏ProgressDialog
     */
    @Override
    public void onCompleted() {
        dismissProgressDialog();
        // Toast.makeText(context, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
    }

    /**
     * 对错误进行统一处理
     * 隐藏ProgressDialog
     *
     * @param e
     */
    @Override
    public void onError(Throwable e) {
//        Log.i("onError",e.getMessage());
//        if (e instanceof SocketTimeoutException) {
//            Toast.makeText(context, "网络中断，请检查您的网络状态", Toast.LENGTH_SHORT).show();
//        } else if (e instanceof ConnectException) {
//            Toast.makeText(context, "网络中断，请检查您的网络状态", Toast.LENGTH_SHORT).show();
//        } else if (e instanceof ApiException){
//            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
//        }else {
//            Toast.makeText(context, "error:" +  e.getMessage(), Toast.LENGTH_SHORT).show();
//        }
        dismissProgressDialog();

    }

    /**
     * 将onNext方法中的返回结果交给Activity或Fragment自己处理
     *
     * @param t 创建Subscriber时的泛型类型
     */
    @Override
    public void onNext(T t) {
        if (mIOnNextListener != null) {
            mIOnNextListener.onNext(t);
        }
    }

//    /**
//     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
//     */
//    @Override
//    public void onCancelProgress() {
//        if (!this.isUnsubscribed()) {
//            this.unsubscribe();
//        }
//    }

    @Override
    public void onButtonClicked(Dialog dialog, int position, Object tag) {

    }

    /**
     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
     */
    @Override
    public void onCancelDialog(Dialog dialog, Object tag) {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }
}